package main
{
	import mx.core.Application;
	/**
	 * Klasse die die Verwaltung der Webcams uebernimmt
	 * @author 3KP
	 * 
	 */
	public class WebcamControl
	{
		import flash.events.NetStatusEvent;
		import flash.media.Camera;
		import flash.media.Video;
		import flash.net.NetConnection;
		import flash.net.NetStream;
		import flash.net.ObjectEncoding;
		import flash.utils.flash_proxy;
		import mx.collections.ArrayCollection;
		import mx.core.UIComponent;
		import mx.core.mx_internal;
		import mx.core.Application;
		private static var video1:Video;
		private static var video2:Video;
		private static var video3:Video;
		private static var video4:Video;
		private static var nc:NetConnection;
		private static var cam_ns:NetStream;
		private static var in_ns1:NetStream;
		private static var in_ns2:NetStream;
		private static var in_ns3:NetStream;
		private static var in_ns4:NetStream;
		private static var mycam:Camera;
		private static var user:String = GameControl.getUser().name;
		private static var userlist:ArrayCollection = GameControl.myUserlist;
		/**
		 * Verbindung zu Red5 herstellen 
		 * 
		 */		
		public static function webcamInitiate():void{
			nc = new NetConnection();
			nc.objectEncoding = ObjectEncoding.AMF0;
			nc.addEventListener( NetStatusEvent.NET_STATUS , netStatus );
			nc.connect("rtmp://192.168.1.65/firstapp", true);
		}
		/**
		 * bei Verbindungserfolg insertWebcamVideo() 
		 * @param event
		 * 
		 */		
		private static function netStatus ( event:NetStatusEvent ):void{	
			trace (event.info.code);
			if(event.info.code =="NetConnection.Connect.Success"){
				insertWebcamVideo();
			}
			
			if ( event.info.code == "NetConnection.Connect.Rejected" ){
				// trace reject message
				trace( event.info.application );
			}
		}
		/**
		 * Methode zur Realisierung der Streams 
		 * 
		 */		
		public static function insertWebcamVideo():void{
			mycam = Camera.getCamera();
			mycam.setMode(320,240,150);
			mycam.setQuality(0,20);
			// eigenen Stream veroeffentlichen		
			cam_ns = new NetStream(nc);
			cam_ns.attachCamera(mycam);
			cam_ns.publish(user, "live");
			
			// Video fuer Position 1
			video1 = new Video();
			if(user == userlist[0]){
				// Wenn Spieler an dieser Position zeige eigene Cam
				video1.attachCamera(mycam);
			} else {
				// Empfangen des Videos fuer Position 1
				in_ns1 = new NetStream(nc);
				in_ns1.play(userlist[0]);
				video1.attachNetStream(in_ns1);
			}
			// Video dem Videoholder zuweisen
			Application.application.pokerEngine.videoHolder1.addChild(video1);	
			
			// Video fuer Position 2
			video2 = new Video();
			if(user == userlist[1]){
				// Wenn Spieler an dieser Position zeige eigene Cam
				video2.attachCamera(mycam);
			} else {
				// Empfangen des Videos fuer Position 2
				in_ns2 = new NetStream(nc);
				in_ns2.play(userlist[1]);
				video2.attachNetStream(in_ns2);
			}
			// Video dem Videoholder zuweisen
			Application.application.pokerEngine.videoHolder2.addChild(video2);
			
			// Video fuer Position 3
			video3 = new Video();
			if(user == userlist[2]){
				// Wenn Spieler an dieser Position zeige eigene Cam
				video3.attachCamera(mycam);
			} else {
				// Empfangen des Videos fuer Position 3
				in_ns3 = new NetStream(nc);
				in_ns3.play(userlist[2]);
				video3.attachNetStream(in_ns3);
			}
			// Video dem Videoholder zuweisen
			Application.application.pokerEngine.videoHolder3.addChild(video3);

			// Video fuer Postion 4
			video4 = new Video();
			if(user == userlist[3]){
				// Wenn Spieler an dieser Position zeige eigene Cam
				video4.attachCamera(mycam);
			} else {
				// Empfangen des Videos fuer Position 4
				in_ns4 = new NetStream(nc);
				in_ns4.play(userlist[3]);
				video4.attachNetStream(in_ns4);
			}
			// Video dem Videoholder zuweisen
			Application.application.pokerEngine.videoHolder4.addChild(video4);
			
		}
	}
}